<?php

namespace app\controller;

use support\Request;
use Kingbes\Attribute\Annotation;
use app\model\AdminRoles;
use app\common\Tool;
use app\common\Tree;
use app\model\Admins;
use app\validate\AdminRolesValidate;
use app\model\Menu;

#[Annotation(title: "角色组", auth: true)]
class AdminRolesController
{
    #[Annotation(title: "权限")]
    public function get_role(Request $request)
    {
        $role = Menu::field("id, title, pid, route, type")
            ->select()->each(function ($v, $k) {
                if ($v->type == "目录") {
                    $v->value = $v->id;
                } else {
                    $v->value = $v->route;
                }
            });
        $Tree = new Tree($role->toArray());
        return success(msg: "获取成功", data: $Tree->buildTree());
    }

    #[Annotation(title: "查询", view: true)]
    public function query(Request $request)
    {
        // 提交
        if ($request->method() === "POST") {
            // 数据
            $param = $request->all();
            // 查询
            $res = Tool::search(AdminRoles::class, $param)
                ->select()
                ->each(function ($v, $k) {
                    if ($v["id"] == 1) {
                        $v["disabled"] = "disabled";
                    }
                });
            // 树形
            $Tree = new Tree($res->toArray());
            // 返回
            return json([
                "code" => 0,
                "msg" => "获取成功",
                "count" => count($res),
                "data" => $Tree->buildTree(),
            ]);
        }
        // 模板渲染
        return fetch();
    }

    #[Annotation(title: "新增", view: true, auth: true)]
    public function add(Request $request)
    {
        // 提交
        if ($request->method() === "POST") {
            // 数据
            $data = $request->all();
            try {
                // 验证
                validate(validate: AdminRolesValidate::class, scene: "add", data: $data);
                // 新增
                $res = AdminRoles::create($data);
                // 返回
                return success(msg: "新增成功", data: $res);
            } catch (\think\exception\ValidateException $e) {
                // 返回验证错误
                return error(
                    msg: $e->getError()["msg"],
                    data: $e->getError()["key"]
                );
            }
        }
        // 输出模板
        return fetch();
    }

    #[Annotation(title: "编辑", auth: true)]
    public function edit(Request $request)
    {
        if ($request->get("id") == 1) {
            return error(msg: "超级管理员不能编辑");
        }
        // 查找
        $find = AdminRoles::find($request->get("id"));
        // 赋值模板
        assign("find", $find);
        // 提交
        if ($request->method() === "POST") {
            // 数据
            $data = $request->all();
            try {
                // 验证
                validate(validate: AdminRolesValidate::class, scene: "edit", data: $data);
                // 更新数据
                $res = AdminRoles::update($data);
                // 返回
                return success(msg: "编辑成功", data: $res);
            } catch (\think\exception\ValidateException $e) {
                // 返回验证错误
                return error(
                    msg: $e->getError()["msg"],
                    data: $e->getError()["key"]
                );
            }
        }
        // 输出模板
        return fetch();
    }

    #[Annotation(title: "删除", auth: true)]
    public function del(Request $request)
    {
        // 数据
        $ids = $request->get("ids");
        // 验证 超级管理员
        if (1 == $ids || (is_array($ids) && in_array(1, $ids))) {
            return error(msg: "超级管理员不能删除");
        }
        // 验证 角色
        $find = Admins::where(function ($query) use ($ids) {
            if (is_array($ids)) {
                $query->where("rid", "in", $ids);
            } else {
                $query->where("rid", $ids);
            }
        })->find();
        if ($find) {
            return error(msg: "角色已被使用");
        }
        // 删除
        $destroy = AdminRoles::destroy($ids);
        // 返回
        if ($destroy) {
            return success("删除成功");
        } else {
            return error(msg: "删除失败", data: $ids);
        }
    }
}
